﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.UnityExtensions;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using TFS.Kanban.Commons.Commands;
using TFS.Kanban.Commons.Extensions;
using TFS.Kanban.Constants;
using TFS.Kanban.Desktop.Installers;
using TFS.Kanban.VS2010.Installers;

namespace TFS.Kanban.Desktop
{
	public class Bootstrapper : UnityBootstrapper
	{
		protected override DependencyObject CreateShell()
		{
			return new MainWindow();
		}

        /// <summary>
        /// Used to register common elements with the container
        /// </summary>
        protected override void ConfigureContainer()
        {
            var navigationCommands = new ObservableCollection<CommandBase>();

            this.Container.RegisterInstance<ICollection<CommandBase>>(CommandNames.NavigationCommands, navigationCommands);
            this.Container.RegisterType<MainWindowViewModel>(new ContainerControlledLifetimeManager(),
                                                             new InjectionConstructor(navigationCommands));

            this.Container.Install(this.Container.Resolve<InfrastructureInstaller>(),
                                   this.Container.Resolve<TFSDomainServiceInstaller>());
    
            base.ConfigureContainer();
        }

		protected override void InitializeModules()
		{
			base.InitializeModules();

		    var window = (Window) this.Shell;
		    window.DataContext = this.Container.Resolve<MainWindowViewModel>();
			Application.Current.MainWindow = window;
			window.Show();

            //TODO: establish better pattern for displaying initial view
		    var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            regionManager.RequestNavigate(RegionNames.Main, "DashboardView");
		}

		protected override void ConfigureModuleCatalog()
		{
			base.ConfigureModuleCatalog();

			this.ModuleCatalog = new DirectoryModuleCatalog { ModulePath = @".\Modules" };
		}
	}
}
